#!/bin/sh
#chkconfig: 2345 80 05
#description: elasticsearch

export runuser={{ os_general_user }}
export elasticsearch_base={{ es_basedir }}
export JAVA_HOME=${elasticsearch_base}/jdk

export PATH=$PATH:${JAVA_HOME}/bin
export CLASSPATH=.:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
export JAVA_HOME JAVA_BIN PATH CLASSPATH



#if [ "${whoami}" != "${runuser}" ];then
#	echo "please use ${runuser} running"
#	exit 0
#fi


case "$1" in
start)
    es_pid=`ps -ef|grep  "Des.path.home=${elasticsearch_base}" | grep -v grep | awk '{print $2}'`
    if [[ -z ${es_pid} ]];then
       ${elasticsearch_base}/bin/elasticsearch -d -p ${elasticsearch_base}/elasticsearch.pid
       echo "elasticsearch startup"
    else
       echo "elasticsearch running"
    fi
    ;;

stop)
    es_pid=`ps -ef|grep  "Des.path.home=${elasticsearch_base}" | grep -v grep | awk '{print $2}'`
    COUNT=0
    SIGNAL=${SIGNAL:-TERM}
    if [ -z "$es_pid" ]; then
      echo "No elasticsearch to stop"
      break
    else
      echo "Stopping elasticsearch ... "
      kill -s $SIGNAL $es_pid
      while (($COUNT<3))
      do
        sleep 3
        es_pid1=`ps -ef|grep  "Des.path.home=${elasticsearch_base}" | grep -v grep | awk '{print $2}'`
          if [[ -z $es_pid1 ]];then
            echo "elasticsearch stopped"
            break
          else [[ $es_pid == $es_pid1 ]]
            es_course=`ps aux |grep  "Des.path.home=${elasticsearch_base}" | grep -v grep |awk '{print $8}'`
            if [[ $es_course == 'Z' ]];then
              kill -9 $es_pid1
              echo "elasticsearch stopped"
              break
            else
              kill -s $SIGNAL $es_pid1
            fi
          fi
        COUNT=`expr $COUNT + 1`
      done
      if [ $COUNT -eq 3 ];then
        kill -9 $es_pid
        if [ -z $es_pid ];then
          echo "elasticsearch stopped"
        else
          echo "elasticsearch stop failed"
        fi
      fi
    fi
    ;;

status)
    es_pid=`ps -ef |grep ${runuser}|grep "Des.path.home=${elasticsearch_base}" | grep -v grep | awk '{print $2}'`
    if [[ $es_pid ]];then echo "elasticsearch is running"; else echo "elasticsearch not running"; fi
    ;;

restart)
    es_pid=`ps -ef|grep ${runuser}|grep "Des.path.home=${elasticsearch_base}" | grep -v grep | awk '{print $2}'`
    COUNT=0
    SIGNAL=${SIGNAL:-TERM}
    if [ -z "$es_pid" ]; then
      echo "No elasticsearch to stop"
      break
    else
      echo "Stopping elasticsearch ... "
      kill -s $SIGNAL $es_pid
      while (($COUNT<3))
      do
        sleep 3
        es_pid1=`ps -ef|grep  "Des.path.home=${elasticsearch_base}" | grep -v grep | awk '{print $2}'`
          if [[ -z $es_pid1 ]];then
            echo "elasticsearch stopped"
            break
          else [[ $es_pid == $es_pid1 ]]
            es_course=`ps aux |grep  "Des.path.home=${elasticsearch_base}" | grep -v grep |awk '{print $8}'`
            if [[ $es_course == 'Z' ]];then
              kill -9 $es_pid1
              echo "elasticsearch stopped"
              break
            else
              kill -s $SIGNAL $es_pid1
            fi
          fi
        COUNT=`expr $COUNT + 1`
      done
      if [ $COUNT -eq 3 ];then
        kill -9 $es_pid
        if [ -z $es_pid ];then
          echo "elasticsearch stopped"
        else
          echo "elasticsearch stop failed"
        fi
      fi
    fi

    ${elasticsearch_base}/bin/elasticsearch -d
    echo "elasticsearch startup"

    ;;

*)
    echo "start|stop|restart|status"
    ;;  
esac
 
exit $?
